export const common = {
  data () {
    return {}
  },
  created () {},
  methods: {
    formatDate (date, fmt) {
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      const o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'H+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
      }
      // eslint-disable-next-line no-unused-vars
      for (const k in o) {
        const str = o[k] + ''
        if (new RegExp(`(${k})`).test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length))
        }
      }
      return fmt
    },
    // 根据字典获取name
    getDictionaryNameByCode (dicName, code) {
      if (this.lookupItems && code) {
        const dicArr = this.lookupItems[dicName] || []
        const findedObj = dicArr.find(item => {
          return item.dicCode === code
        })
        if (findedObj) return findedObj.dicName
      }
      return code
    },
    getLookupItems (type) {
      const _lookupItems = this.lookupItems
      if (_lookupItems) {
        for (var i = 0; i < _lookupItems.length; i++) {
          if (_lookupItems[i].key === type) {
            return _lookupItems[i].children
          }
        }
      }
    },
    permission (...roles) {
      const _roles = this.roles
      if (_roles) {
        for (var i = 0; i < _roles.length; i++) {
          for (var j = 0; j < roles.length; j++) {
            if (_roles[i] === roles[j]) {
              return true
            }
          }
        }
      }
      return false
    },
    getDicLang (val) {
      if (this.$i18n.locale === 'zh' && val.languages) {
        return (val.languages.CN ? val.languages.CN.dicName : val.dicName)
      } else if (this.$i18n.locale === 'en' && val.languages) {
        return (val.languages.EN.dicName)
      } else {
        return val.dicName
      }
    },
    // 判断金额参数是否为0，如果为0则显示参数
    getDisplayData (data) {
      if (data || data === 0) {
        return true
      } else {
        return false
      }
    },
    // 保留两位小数
    getPoint (data) {
      if (data) {
        return data.toFixed(2)
      } else if (data === 0) {
        return '0.00'
      }
    },
    getNameById (id, list, idKey = 'id', nameKey = 'name') {
      if (id && list) {
        const obj = list.find(item => item[idKey] === id)
        if (obj) {
          return obj[nameKey]
        } else {
          return ''
        }
      }
    },
    getNamesByIdList (idList, objList, idKey = 'id', nameKey = 'name') {
      if (idList && objList) {
        const nameList = []
        for (let i = 0; i < idList.length; i++) {
          const item = this.getNameById(idList[i], objList, idKey, nameKey)
          if (item !== '') {
            nameList.push(item)
          }
        }
        return nameList.join(',')
      }
    },
    operationPageBack (pageName, params) {
      this.$store.dispatch('delCachedView', { name: pageName }).then(_ => {
        this.$router.push({
          name: pageName,
          params: params || {}
        })
      })
    }
  }
}
